package com.wuwei.elearning.utils;

import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class MyArrayUtils {
    public static boolean isConsecutive(List<Integer> list) {
        Collections.sort(list);
        int size = list.size();
        if (size == 0) { // 处理空列表的情况
            return true;
        }
        int first = list.get(0);
        if (first != 1) { // 列表的第一个元素必须为1
            return false;
        }
        for (int i = 1; i < size; i++) {
            int current = list.get(i);
            if (current != first + 1) {
                return false;
            }
            first = current;
        }
        return true;
    }

    public static <T> HashMap<String,Object> findDifferences(T[] arrayA, T[] arrayB) {
        // 将数组转换为Set
        Set<T> setB = new HashSet<>(Arrays.asList(arrayB));

        // A 中独有的元素
        List<T> uniqueInA = Arrays.stream(arrayA)
                .filter(element -> !setB.contains(element))
                .collect(Collectors.toList());

        Set<T> setA = new HashSet<>(Arrays.asList(arrayA));

        // B 中独有的元素
        List<T> uniqueInB = Arrays.stream(arrayB)
                .filter(element -> !setA.contains(element))
                .collect(Collectors.toList());

        HashMap<String,Object> map = new HashMap<>();
        map.put("A", uniqueInA);
        map.put("B", uniqueInB);
        return map;
    }
}
